package com.example.waimai.ui.notifications;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.example.waimai.MainActivity;
import com.example.waimai.context.UserInfoContext;
import com.example.waimai.pojo.User;
import com.example.waimaiandroid.R;
import com.example.waimaiandroid.address.AddressActivity;
import com.example.waimaiandroid.login.LoginActivity;
import com.example.waimaiandroid.updateInfo.updateInfo;

public class NotificationsFragment extends Fragment {

    private TextView UserName;
    private TextView UserTelephone;
    private TextView myOrder;
    private TextView myAddress;
    private TextView updateUserInfo;
    private TextView exitLogin;
    private ImageView userImage;

    //private User user;

    private View view ;

    public static NotificationsViewModel notificationsViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);
        view = inflater.inflate(R.layout.fragment_notifications, null, false);
        //user = notificationsViewModel.getUserInfo().getValue();
        initView();

        notificationsViewModel.getUserInfo().observe(getViewLifecycleOwner(), new Observer<User>() {
            @Override
            public void onChanged(User user) {
                UserName.setText(user.getUserName());
                UserTelephone.setText(user.getTelephone());
               Glide.with(view).load(user.getUserImage()).into(userImage);
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到地址管理页面
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(MainActivity.getMainActivity(), AddressActivity.class));
                MainActivity.getMainActivity().startActivity(intent);
            }
        });

        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = MainActivity.getMainActivity().getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.clear();
                edit.commit();
                // 跳转到登录界面
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(MainActivity.getMainActivity(), LoginActivity.class));
                MainActivity.getMainActivity().startActivity(intent);
            }
        });
        updateUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到修改页面去
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(MainActivity.getMainActivity(), updateInfo.class));
                MainActivity.getMainActivity().startActivity(intent);
            }
        });

    }

    private void initView(){
        UserName =  view.findViewById(R.id.UserName);
        UserTelephone =  view.findViewById(R.id.UserTelephone);
        myOrder =  view.findViewById(R.id.myOrder);
        myAddress =  view.findViewById(R.id.myAddress);
        updateUserInfo =  view.findViewById(R.id.updateUserInfo);
        exitLogin =  view.findViewById(R.id.exitLogin);
        userImage = view.findViewById(R.id.UserImage);

    }

    private void initListener(){

    }
}